package com.hg.chapter84.service.impl;

import com.hg.chapter84.entity.Worker;
import com.hg.chapter84.repository.WorkerRepository;
import com.hg.chapter84.service.WorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class WorkerServiceImpl implements WorkerService {

    @Autowired
    private WorkerRepository workerRepository;

    @Override
    @CachePut(value = "worker",key = "#worker.id")
    public Worker save(Worker worker) {
        return workerRepository.save(worker);
    }

    @Override
    @CacheEvict(value = "worker")
    public void remove(Long id) {
        workerRepository.deleteById(id);
    }

    @Override
    @Cacheable(value = "worker",key = "#worker.id")
    public Worker findOne(Worker worker) {
        return workerRepository.findById(worker.getId()).get();
    }
}
